package com.anyonavinfo.cpaddemo.view.settingsview.wifipw;

import android.os.Handler;
import android.os.Looper;


import com.anyonavinfo.cpaddemo.data.hostinfo.source.service.E3HWServiceHelper;
import com.anyonavinfo.cpaddemo.utils.WifiPwVerifyUtils;


import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by ZuiC on 2017/2/4.
 */

public class WifiPwSettingPresenter implements WifiPwSettingContract.Presenter,E3HWServiceHelper.ServiceObserver {

    private E3HWServiceHelper serviceHelper;
    private String curWifiPassword = null;
    private int isPadConnected = -1;
    private WifiPwSettingContract.View mView;
    private String changeResult;
    private Handler mHandler = new Handler(Looper.getMainLooper());


    public WifiPwSettingPresenter(WifiPwSettingContract.View view, E3HWServiceHelper serviceHelper){
        this.mView = view;
        mView.setPresenter(this);
        this.serviceHelper = serviceHelper;
    }

    @Override
    public void start() {
        this.serviceHelper = E3HWServiceHelper.getServiceHelperInstance();
        this.serviceHelper.registerObserver(this);
    }

    @Override
    public void submitPassWord(final String newPW, String confirmPw) {
        changeResult = null;
        mView.showWifiPwSettingView(false);
        mView.showProgressBar(true);
        if (isPadConnected == 0){
            changeResult = "修改密码失败！";
        }

        String verifyResult = new WifiPwVerifyUtils().verifyPw(newPW, confirmPw);
        if (verifyResult != null){
            changeResult = verifyResult;
        }

        if (changeResult != null){
            mView.showProgressBar(false);
            mView.showResult(changeResult, true);
            return;
        }

        serviceHelper.ExecCommand(serviceHelper.SET_WIFIPASSWORD, newPW);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (curWifiPassword != null && curWifiPassword.equals(newPW)){
                    changeResult = "修改成功！";
                    curWifiPassword = null;
                }else {
                    changeResult = "修改密码失败！";
                }
                mView.showProgressBar(false);
                mView.showResult(changeResult, true);
            }
        },3000);
    }

    @Override
    public void confirmResultInfo() {
        mView.showResult(null, false);
        mView.showWifiPwSettingView(true);
    }

    @Override
    public void onResetPassWord() {
        mView.showWifiPwSettingView(false);
        mView.showConfirmResetPwView(true);
    }

    @Override
    public void resetPassWord() {
        mView.showConfirmResetPwView(false);
        changeResult = null;
        mView.showProgressBar(true);
        if (isPadConnected == 0){
            changeResult = "修改密码失败！";
        }

        if (changeResult != null){
            mView.showProgressBar(false);
            mView.showResult(changeResult, true);
        }

        serviceHelper.ExecCommand(serviceHelper.SET_WIFIPASSWORD, "12345678");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (curWifiPassword != null && curWifiPassword.equals("12345678")){
                    changeResult = "修改成功！";
                    curWifiPassword = null;
                }else {
                    changeResult = "修改密码失败！";
                }
                mView.showProgressBar(false);
                mView.showResult(changeResult, true);
            }
        },3000);

    }

    @Override
    public void cancelResetPw() {
        mView.showConfirmResetPwView(false);
        mView.showWifiPwSettingView(true);
    }

    @Override
    public void update(String json) {
        String JsonID = "";
        JSONObject jsonparas;
        JSONObject jsonobject;

        try {
            jsonobject = new JSONObject(json.toString());
            JsonID = jsonobject.names().getString(0);
            if (JsonID.endsWith("WIFIPASSWORDINFO")){
                jsonparas = new JSONObject(
                        jsonobject.getString("WIFIPASSWORDINFO"));
                curWifiPassword = jsonparas.getString("wifiPassword");
            }else if(JsonID.endsWith("ISPADCONNECT")){
                jsonparas = new JSONObject(jsonobject.getString("ISPADCONNECT"));
                isPadConnected = Integer.parseInt(jsonparas.getString("isPadConnect"));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
